import '../model/chat_message_model.dart';

/// 聊天消息默认数据
/// 提供默认的聊天消息数据
class ChatMessageData {
  /// 获取所有默认消息数据
  static List<ChatMessageModel> getDefaultMessages() {
    final now = DateTime.now();

    return [
      // Group 1 的消息
      ChatMessageModel(
        id: '1',
        groupId: '1',
        senderId: '1', // admin
        content: 'Welcome to our Green Plant Care Exchange Group! 🌱',
        sendTime: now.subtract(const Duration(days: 9, hours: 2)),
      ),
      ChatMessageModel(
        id: '2',
        groupId: '1',
        senderId: '2', // gardenmaster
        content:
            'Thanks for creating this group! I have some great tips to share about pothos care.',
        sendTime: now.subtract(const Duration(days: 8, hours: 5)),
      ),
      ChatMessageModel(
        id: '3',
        groupId: '1',
        senderId: '3', // leafyfriend
        content: 'My pothos leaves are turning yellow. Any suggestions?',
        sendTime: now.subtract(const Duration(days: 7, hours: 3)),
      ),
      ChatMessageModel(
        id: '4',
        groupId: '1',
        senderId: '4', // plantcare
        content:
            'Yellow leaves usually mean overwatering. Let the soil dry out between waterings!',
        sendTime: now.subtract(const Duration(days: 7, hours: 2)),
      ),
      ChatMessageModel(
        id: '5',
        groupId: '1',
        senderId: '5', // greensoul
        content:
            'I agree! Also check if it\'s getting enough light. Pothos like bright, indirect light.',
        sendTime: now.subtract(const Duration(days: 6, hours: 8)),
      ),

      // Group 2 的消息
      ChatMessageModel(
        id: '6',
        groupId: '2',
        senderId: '2', // gardenmaster
        content: 'Welcome succulent lovers! 🌵 Show off your beautiful plants!',
        sendTime: now.subtract(const Duration(days: 7, hours: 4)),
      ),
      ChatMessageModel(
        id: '7',
        groupId: '2',
        senderId: '3', // leafyfriend
        content: 'I just got a new echeveria! Any propagation tips?',
        sendTime: now.subtract(const Duration(days: 6, hours: 12)),
      ),
      ChatMessageModel(
        id: '8',
        groupId: '2',
        senderId: '6', // blooming
        content:
            'For echeveria propagation, let the leaf callus over for a few days before placing on soil.',
        sendTime: now.subtract(const Duration(days: 6, hours: 10)),
      ),
      ChatMessageModel(
        id: '9',
        groupId: '2',
        senderId: '7', // plantlife
        content:
            'I love using terracotta pots for succulents - they help with drainage!',
        sendTime: now.subtract(const Duration(days: 5, hours: 6)),
      ),

      // Group 3 的消息
      ChatMessageModel(
        id: '10',
        groupId: '3',
        senderId: '3', // leafyfriend
        content: 'Let\'s turn our balconies into beautiful flower gardens! 🌸',
        sendTime: now.subtract(const Duration(days: 5, hours: 8)),
      ),
      ChatMessageModel(
        id: '11',
        groupId: '3',
        senderId: '1', // admin
        content: 'Great idea! What flowers work best for balcony gardening?',
        sendTime: now.subtract(const Duration(days: 5, hours: 6)),
      ),
      ChatMessageModel(
        id: '12',
        groupId: '3',
        senderId: '5', // greensoul
        content:
            'Petunias, marigolds, and geraniums are perfect for balconies!',
        sendTime: now.subtract(const Duration(days: 4, hours: 9)),
      ),
      ChatMessageModel(
        id: '13',
        groupId: '3',
        senderId: '7', // plantlife
        content:
            'Don\'t forget about herbs! Basil and mint grow great in containers.',
        sendTime: now.subtract(const Duration(days: 3, hours: 4)),
      ),

      // Group 4 的消息
      ChatMessageModel(
        id: '14',
        groupId: '4',
        senderId: '4', // plantcare
        content:
            'Welcome beginners! Don\'t be afraid to ask questions - we\'re here to help! 🌱',
        sendTime: now.subtract(const Duration(days: 3, hours: 10)),
      ),
      ChatMessageModel(
        id: '15',
        groupId: '4',
        senderId: '1', // admin
        content: 'What are the essential tools for a beginner gardener?',
        sendTime: now.subtract(const Duration(days: 3, hours: 8)),
      ),
      ChatMessageModel(
        id: '16',
        groupId: '4',
        senderId: '2', // gardenmaster
        content:
            'Start with a good watering can, hand trowel, and pruning shears!',
        sendTime: now.subtract(const Duration(days: 3, hours: 6)),
      ),
      ChatMessageModel(
        id: '17',
        groupId: '4',
        senderId: '6', // blooming
        content:
            'I recommend starting with easy plants like spider plants or pothos.',
        sendTime: now.subtract(const Duration(days: 2, hours: 12)),
      ),
      ChatMessageModel(
        id: '18',
        groupId: '4',
        senderId: '8', // naturelover
        content: 'Don\'t worry about making mistakes - we all learn by doing!',
        sendTime: now.subtract(const Duration(days: 2, hours: 8)),
      ),

      // Group 5 的消息
      ChatMessageModel(
        id: '19',
        groupId: '5',
        senderId: '5', // greensoul
        content:
            'Indoor plant lovers unite! Let\'s share our best low-light plant tips! 🪴',
        sendTime: now.subtract(const Duration(days: 1, hours: 14)),
      ),
      ChatMessageModel(
        id: '20',
        groupId: '5',
        senderId: '1', // admin
        content:
            'My peace lily is thriving in my office! Perfect for low light.',
        sendTime: now.subtract(const Duration(days: 1, hours: 12)),
      ),
      ChatMessageModel(
        id: '21',
        groupId: '5',
        senderId: '3', // leafyfriend
        content:
            'Monstera deliciosa is my favorite! It\'s so dramatic and easy to care for.',
        sendTime: now.subtract(const Duration(days: 1, hours: 8)),
      ),
      ChatMessageModel(
        id: '22',
        groupId: '5',
        senderId: '7', // plantlife
        content: 'Snake plants are indestructible! Perfect for beginners.',
        sendTime: now.subtract(const Duration(hours: 18)),
      ),

      // Group 6 的消息
      ChatMessageModel(
        id: '23',
        groupId: '6',
        senderId: '6', // blooming
        content: 'Share your flower growing secrets here! 🌺',
        sendTime: now.subtract(const Duration(hours: 10)),
      ),
      ChatMessageModel(
        id: '24',
        groupId: '6',
        senderId: '2', // gardenmaster
        content: 'Deadheading regularly helps flowers bloom longer!',
        sendTime: now.subtract(const Duration(hours: 8)),
      ),
      ChatMessageModel(
        id: '25',
        groupId: '6',
        senderId: '4', // plantcare
        content: 'I use coffee grounds as fertilizer - my roses love it!',
        sendTime: now.subtract(const Duration(hours: 6)),
      ),
      ChatMessageModel(
        id: '26',
        groupId: '6',
        senderId: '8', // naturelover
        content: 'Old egg cartons make great seed starters!',
        sendTime: now.subtract(const Duration(hours: 4)),
      ),
      ChatMessageModel(
        id: '27',
        groupId: '6',
        senderId: '6', // blooming
        content: 'Great tip! I also use toilet paper rolls for seedlings.',
        sendTime: now.subtract(const Duration(hours: 2)),
      ),
    ];
  }

  /// 根据群聊ID获取消息
  static List<ChatMessageModel> getMessagesByGroupId(String groupId) {
    return getDefaultMessages()
        .where((message) => message.groupId == groupId)
        .toList()
      ..sort((a, b) => a.sendTime.compareTo(b.sendTime));
  }

  /// 根据发送者ID获取消息
  static List<ChatMessageModel> getMessagesBySenderId(String senderId) {
    return getDefaultMessages()
        .where((message) => message.senderId == senderId)
        .toList()
      ..sort((a, b) => b.sendTime.compareTo(a.sendTime));
  }

  /// 获取群聊的最新消息
  static ChatMessageModel? getLatestMessageByGroupId(String groupId) {
    final messages = getMessagesByGroupId(groupId);
    return messages.isNotEmpty ? messages.last : null;
  }

  /// 获取消息统计信息
  static Map<String, int> getMessageStatistics() {
    final messages = getDefaultMessages();
    final stats = <String, int>{};

    // 总消息数
    stats['totalMessages'] = messages.length;

    // 各群聊消息数
    for (int i = 1; i <= 6; i++) {
      stats['group${i}Messages'] = getMessagesByGroupId(i.toString()).length;
    }

    // 各用户消息数
    for (int i = 1; i <= 8; i++) {
      stats['user${i}Messages'] = getMessagesBySenderId(i.toString()).length;
    }

    return stats;
  }

  /// 根据ID获取消息
  static ChatMessageModel? getMessageById(String id) {
    try {
      return getDefaultMessages().firstWhere((message) => message.id == id);
    } catch (e) {
      return null;
    }
  }

  /// 获取最新消息
  static List<ChatMessageModel> getLatestMessages() {
    final messages = getDefaultMessages();
    messages.sort((a, b) => b.sendTime.compareTo(a.sendTime));
    return messages;
  }
}
